#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "binary_tree.h"

ret_e deal_int(int data,void *context)
{
	printf("%d\t",data);

	if (context != NULL) {
	*(int *)context = data;
	}
	
	return RET_OK;
}
void tree_to_dlist_test(void)
{
	binary_tree_s *tree;
	binary_tree_node_s *build_tree;
	ret_e ret;
	int pre[100];
	int mid[100];
	int len;

	tree = binary_tree_create();
	assert_debug(tree != NULL);

	ret = binary_tree_add_data(tree, 10);
	assert_debug(ret == RET_OK);
	ret = binary_tree_add_data(tree, 6);
	assert_debug(ret == RET_OK);
	ret = binary_tree_add_data(tree, 12);
	assert_debug(ret == RET_OK);
	ret = binary_tree_add_data(tree, 4);
	assert_debug(ret == RET_OK);
	ret = binary_tree_add_data(tree, 8);
	assert_debug(ret == RET_OK);
	ret = binary_tree_add_data(tree, 14);
	assert_debug(ret == RET_OK);
	ret = binary_tree_add_data(tree, 11);
	assert_debug(ret == RET_OK);
	
	
	len = binary_tree_mid_out_by_non_recursion(tree->root, deal_int,(void **)mid);
	putchar('\n');
	binary_tree_pre_out_by_non_recursion(tree->root, deal_int,(void **)pre);
	putchar('\n');
	build_tree = binary_tree_build_by_mid_and_pre(pre, mid,len);
	putchar('\n');
	binary_tree_mid_out_by_recursion(build_tree, deal_int);
	putchar('\n');
	binary_tree_pre_out_by_recursion(build_tree, deal_int);
	
}
void tree_to_dlist_test_rand(int argc)
{
	int i;
	binary_tree_s *tree;
	ret_e ret;

	tree = binary_tree_create();
	assert_debug(tree != NULL);

	srand(time(NULL));
	for (i=0;i<argc; i++) {
		ret = binary_tree_add_data(tree, rand()%argc);
		assert_debug(ret == RET_OK);
	}
	binary_tree_print(tree,deal_int);
	dlist_for_loop(tree_to_dlist(tree),deal_int);
}
int main(int argc,char **argv)
{
	if (argc != 2) {
		tree_to_dlist_test();
	} else {
		tree_to_dlist_test_rand(atoi(argv[1]));
	}
	return 0;
}

